package com.medical.medical.controller.dmyController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.medical.medical.entity.Booking;
import com.medical.medical.entity.Department;
import com.medical.medical.entity.Doctor;
import com.medical.medical.entity.WorkForces;
import com.medical.medical.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("dmy")
@Controller
public class PatientController {

    @Resource
    private PatientService patientService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private DoctorService doctorService;
    @Resource
    private WorkForcesService workForcesService;
    @Resource
    private BookingService bookingService;

    /**
     * 用户选定科室,医生进行挂号,
     * @param workforcesDepartment
     * @param workforcesDoctor
     * @param resultMap
     * @return
     */
    @RequestMapping("/doBooking")
    public Object doBooking(@RequestParam(value = "workforcesDepartment",required = false) int workforcesDepartment,
                            @RequestParam(value = "workforcesDoctor",required = false) int workforcesDoctor,
                            Map<String,Object> resultMap){
        //@RequestParam(value = "departmentName",required = false)
        //@RequestParam(value = "dName",required = false)
        log.info("用户患者根据科室名,医生名开始挂号...");
        Map<String, Object> result = doctorService.doBookingBydepartmentNameAndDName(workforcesDepartment,workforcesDoctor);
        resultMap.putAll(result);
        log.info("结果:{}",result.get("message"));
        return JSONArray.toJSON(resultMap);
    }


    /**
     * 跳转到主页面
     * @return
     */
    @RequestMapping("/userMain")
    public String touserMain(){
        return "user/userMain";
    }

    /**
     * 跳转科室名称页面
     */
    @RequestMapping("/deparName")
    public String todeparName(){
        return "user/deparName";
    }

    /**
     * 预约-医生
     * @return
     */
    @RequestMapping("/touserbooking")
    public String touserbooking(String departmentId,Model model){
        model.addAttribute("departmentId",departmentId);
        log.info("预约-医生");
        return "user/userbooking";
    }
    @RequestMapping("/getDocListByDepartmentId")
    @ResponseBody
    public Object getDocListByDepartmentId(String departmentId){
        log.info("查询科室里专家/医生信息...");
        List<Doctor> doctorList = doctorService.getDocListByDepartmentId(Integer.valueOf(departmentId));
        log.info("{}",doctorList);
        log.info("{}",JSONArray.toJSONString(doctorList));
        return JSONArray.toJSONString(doctorList);
    }


    /**
     * 预约-排班
     * @return
     */
    @RequestMapping("/touserBookWork")
    public String touserBookWork(String workforcesDepartment,String  workforcesDoctor,Model model){
        model.addAttribute("workforcesDepartment",workforcesDepartment);
        model.addAttribute("workforcesDoctor",workforcesDoctor);
        log.info("预约-排班");
        return "user/userBookWork";
    }

    @RequestMapping({"/","/getWordByKidAndDid"})
    @ResponseBody
    public String appointByKname(String workforcesDepartment, String workforcesDoctor){
        log.info("查询排班");
        List<WorkForces> workForcesList = workForcesService.getWordByKidAndDid(Integer.valueOf(workforcesDepartment),Integer.valueOf(workforcesDoctor));
        log.info("{}",JSONArray.toJSONString(workForcesList));
        return JSONArray.toJSONString(workForcesList);
    }

    @RequestMapping("/toaddBook")
    public String toaddBook(String time,Model model){
        log.info("预约-排班");
        model.addAttribute("time",time);
        return "user/addPat";
    }
    @RequestMapping({"/","/booking"})
    @ResponseBody
    public String addBooking(Booking booking,String time) throws ParseException {
        log.info("预约功能");
        boolean addFlag = bookingService.addBooking(booking);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = format.parse(time);
        booking.setWorkforcesTime(parse);
        if (addFlag){
            return "success";
        }
        return "fail";
    }

    @RequestMapping({"/","/getDepartmentTwo"})
    public String getDepartmentTwo(Model model){
        log.info("查询科室开始");
        List<Department> departmentList = departmentService.getDepartmentList();
        model.addAttribute("departmentList",departmentList);
        return "wld/departmentlistTwo";
    }






}